package com.haidong;

import com.haidong.mapper.*;
import com.haidong.pojo.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class SqlFunction {
    private static String resource = "mybatis-config.xml";
    private static InputStream inputStream;
    //配置数据库接口
    static {
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    private static SqlSession sqlSession = sqlSessionFactory.openSession();
    //房子管理的数据库操作
    public static List<House> selectHouse(House house){
        HouseMapper hm = sqlSession.getMapper(HouseMapper.class);
        return hm.selectByCondition(house);
    }
    public static void insertHouse(House house){
        HouseMapper hm = sqlSession.getMapper(HouseMapper.class);
        hm.insert(house);
        sqlSession.commit();
    }
    public static void deleteHouse(int id){
        HouseMapper hm = sqlSession.getMapper(HouseMapper.class);
        hm.delete(id);
        sqlSession.commit();
    }
    public static void updateHouse(House house){
        HouseMapper hm = sqlSession.getMapper(HouseMapper.class);
        hm.update(house);
        sqlSession.commit();
    }
    //工人管理数据库操作
    public static List<Worker> selectWorker(Worker worker){
        WorkerMapper wm = sqlSession.getMapper(WorkerMapper.class);
        return wm.selectByCondition(worker);
    }
    public static void insertWorker(Worker worker){
        WorkerMapper wm = sqlSession.getMapper(WorkerMapper.class);
        wm.insert(worker);
        sqlSession.commit();
    }
    public static String idToDepart(int id){
        DepartmentMapper dm = sqlSession.getMapper(DepartmentMapper.class);
        Department department = dm.selectById(id);
        return department.getName();
    }
    public static String idToHouseName(Integer id){
        HouseMapper hm = sqlSession.getMapper(HouseMapper.class);
        House house = hm.selectById(id);
        return house.getName();
    }
    public static void deleteWorker(int id){
        WorkerMapper wm = sqlSession.getMapper(WorkerMapper.class);
        wm.delete(id);
        sqlSession.commit();
    }
    public static void updateWorker(Worker worker){
        WorkerMapper wm = sqlSession.getMapper(WorkerMapper.class);
        wm.update(worker);
        sqlSession.commit();
    }
    //物业费管理数据库操作
    public static HouseCost selectByIdCost(int id){
        CostMapper costMapper = sqlSession.getMapper(CostMapper.class);
        HouseMapper houseMapper = sqlSession.getMapper(HouseMapper.class);
        NormPriceMapper normPriceMapper = sqlSession.getMapper(NormPriceMapper.class);
        List<Cost> c = costMapper.selectByHouseTime(id, LocalDate.now().getYear(), LocalDate.now().getMonthValue());
        Cost cost = c.get(0);
        House house = houseMapper.selectById(cost.getHouseId());
        NormPrice normPrice = normPriceMapper.selectById(house.getHouseLevel());
        Double costs = ((cost.getWaterNow() - cost.getWaterBase()) * normPrice.getWaterPrice() +
                (cost.getElectNow() - cost.getElectBase()) * normPrice.getElectPrice() +
                (cost.getGasNow() - cost.getGasBase()) * normPrice.getGasPrice() +
                house.getArea() * normPrice.getPrice());
        return new HouseCost(id,house.getName(),costs);
    }
    public static List<HouseCost> selectAllCost(){
        CostMapper costMapper = sqlSession.getMapper(CostMapper.class);
        HouseMapper houseMapper = sqlSession.getMapper(HouseMapper.class);
        NormPriceMapper normPriceMapper = sqlSession.getMapper(NormPriceMapper.class);
        List<Cost> costs = costMapper.selectByTime(LocalDate.now().getYear(), LocalDate.now().getMonthValue());
        List<HouseCost> hcs = new ArrayList<>();
        for (Cost cost : costs) {
            House house = houseMapper.selectById(cost.getHouseId());
            NormPrice normPrice = normPriceMapper.selectById(house.getHouseLevel());
            Double cost1 = ((cost.getWaterNow() - cost.getWaterBase()) * normPrice.getWaterPrice() +
                    (cost.getElectNow() - cost.getElectBase()) * normPrice.getElectPrice() +
                    (cost.getGasNow() - cost.getGasBase()) * normPrice.getGasPrice() +
                    house.getArea() * normPrice.getPrice());
            hcs.add(new HouseCost(house.getId(),house.getName(),cost1));
        }
        return hcs;
    }
    public static List<NormPrice> selectAllNormPrice(){
        NormPriceMapper npm = sqlSession.getMapper(NormPriceMapper.class);
        return npm.selectAll();
    }
    public static List<HouseCost> selectConditionCost(Integer id,Integer year,Integer month){
        CostMapper costMapper = sqlSession.getMapper(CostMapper.class);
        HouseMapper houseMapper = sqlSession.getMapper(HouseMapper.class);
        NormPriceMapper normPriceMapper = sqlSession.getMapper(NormPriceMapper.class);
        List<Cost> costs = costMapper.selectByHouseTime(id,year,month);
        List<HouseCost> hcs = new ArrayList<>();
        for (Cost cost : costs) {
            House house = houseMapper.selectById(cost.getHouseId());
            NormPrice normPrice = normPriceMapper.selectById(house.getHouseLevel());
            Double cost1 = ((cost.getWaterNow() - cost.getWaterBase()) * normPrice.getWaterPrice() +
                    (cost.getElectNow() - cost.getElectBase()) * normPrice.getElectPrice() +
                    (cost.getGasNow() - cost.getGasBase()) * normPrice.getGasPrice() +
                    house.getArea() * normPrice.getPrice());
            hcs.add(new HouseCost(house.getId(),house.getName(),cost1));
        }
        return hcs;
    }
    public static void updateNormPrice(NormPrice np){
        NormPriceMapper npm = sqlSession.getMapper(NormPriceMapper.class);
        npm.update(np);
        sqlSession.commit();
    }
}
